package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import commands.CheckNewLoan;
import commands.LoginCheckCommand;
import entities.Student;

/**
 * 
 * @author eks691
 * 
 */
public class EmployeeNewLoanPanel extends JPanel {

	/**
	 * The list of new loans
	 */
	JList dataList;

	/**
	 * The model for the list
	 */
	DefaultListModel model;

	/**
	 * Holds all the new loans for the student
	 */
	Vector<Student> data;

	/** id of either the student, manager, or employee in the system */
	private int id;

	private static final long serialVersionUID = 1L;

	public EmployeeNewLoanPanel(int idNum) {
		id = idNum;

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBorder(new TitledBorder(new EtchedBorder(), "New Student Loans"));
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		// add(Box.createVerticalStrut(50));

		// Create a JList that displays the Vector in data[]
		// The value of the JList model property is an object that provides
		// a read-only view of the data. It was constructed automatically.

		CheckNewLoan cnl = new CheckNewLoan();

		// setLayout(new BorderLayout());
		model = new DefaultListModel();

		data = cnl.getNewLoans();
		// String[] data = {"one", "two", "three", "four"};
		dataList = new JList(model);

		// create scrollbar
		JScrollPane scrollPane = new JScrollPane(dataList);

		// list = new JList(model);
		for (int i = 0; i < data.size(); i++) {
			model.addElement(data.elementAt(i).getName());
		}

		// listPanel.add(scrollPane);
		add(scrollPane);
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());

		// create a panel for the buttons
		JPanel buttonPanel = new JPanel();
		// GridLayout(rows, columns, horz gap, vertical gap) - gaps are between
		// each row/column
		buttonPanel.setLayout(new GridLayout(1, 3, 150, 150));
		// create buttons
		JButton newLoanButton = new JButton("Select");
		newLoanButton.setFont(new Font("Serif", Font.BOLD, 24));

		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		buttonPanel.add(new JLabel(""));
		buttonPanel.add(new JLabel(""));
		buttonPanel.add(newLoanButton);
		// buttonPanel.add(new JLabel(""));
		add(buttonPanel, BorderLayout.PAGE_END);

		// action listeners for the buttons
		newLoanButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				// get the item clicked on
				int index = dataList.getSelectedIndex();
				System.out.println("Index selected: " + index);

				if (index != -1) {

					String selectedStudentName = (String) dataList.getModel()
							.getElementAt(index);
					System.out.println("Student selected: "
							+ selectedStudentName);

					Student selectedStudent = data.elementAt(index);

					// switch panes
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame frame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					frame.setVisible(true);
					EmployeeNewStudentLoanInfoPanel newPanel = new EmployeeNewStudentLoanInfoPanel(
							selectedStudent, id);
					frame.changePanel(newPanel);
				} else {
					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,
							"Please select a new loan to evaluate");
				}
			}
		});

	}

}
